const jwt = require('jsonwebtoken');
const config = require('../config/environment');
const util = require('util');
const verify = util.promisify(jwt.verify);

/**
 * 判断token是否可用
 */
module.exports = function () {
  return async function (ctx, next) {
    try {
      // 获取jwt
        console.log(ctx.request);
        let {method} = ctx.request;
        if (method.toLowerCase() !== 'options'){
            const token = ctx.header.authorization;
            console.log('token',token);
            if (token) {
                try {
                    // 解密payload，获取用户名和ID

                    let payload = await verify(token.split(' ')[1], config.tokenSecret);

                    console.log('payload'+ JSON.stringify(payload));
                    ctx.user = {
                        name: payload.name,
                        id: payload.id
                    };
                } catch (err) {
                    console.log('token verify fail: ', err)
                }
            }
            await next();
        }else{
            ctx.status = 200;
            ctx.body = {
                success: 200,
                message: '认证成功'
            };
        }
    } catch (err) {
      if (!err || err === {}) return;
      if (err.status === 401) {
        ctx.status = 401;
        ctx.body = {
          success: 0,
          message: '认证失败'
        };
      } else {
        err.status = 404;
        ctx.body = {
          success: 0,
          message: '404 '+ err.toString()
        };
      }
    }
  }
}
